package com.frank.loadinglibrary.special.cool.letter;


import com.frank.loadinglibrary.special.cool.CoolLoadingComponent;
import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;

/**
 * Created by Weiwu on 16/2/20.
 */
public class DLetter extends Letter {

    private Paint mPaint;

    private RectFloat mRectF;
    //圆角度起点
    private int mStartAngle = 0;
    //变化角度
    private int mSweepAngle = 0;
    //字母边长
    private int mRadius = 120;
    private int mLineLength = 180;
    //线粗
    private int mStrokeWidth = 20;
    private AnimatorValue mAnimator;

    private Point mFirPoint;
    private Point mSecPoint;

    public DLetter(float x, float y) {
        super(x, y);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(CoolLoadingComponent.WHITE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);

        //除去线粗带来的偏差
        int offsetSub = mRadius / 2 - mStrokeWidth / 2;
        //对其圆右边内边位置
        mFirPoint = new Point(mCurX + offsetSub, mCurY + mRadius / 2);
        mSecPoint = new Point(mFirPoint);
        //圆向内偏移
        mRectF = new RectFloat(mCurX - offsetSub, mCurY - offsetSub, mCurX + offsetSub, mCurY + offsetSub);

    }

    public void reset() {
        if (mPaint != null) {
            mPaint.reset();
            mPaint.setAntiAlias(true);
            mPaint.setColor(CoolLoadingComponent.WHITE);
            mPaint.setStrokeWidth(mStrokeWidth);
            mPaint.setStyle(Paint.Style.STROKE_STYLE);
        }
        mStartAngle = 0;
        mSweepAngle = 0;
        mRadius = 120;
        mLineLength = 180;
        mStrokeWidth = 20;
        //除去线粗带来的偏差
        int offsetSub = mRadius / 2 - mStrokeWidth / 2;
        //对其圆右边内边位置
        if (mFirPoint != null) {
            mFirPoint.modify(mCurX + offsetSub, mCurY + mRadius / 2);
        }
        if (mSecPoint != null) {
            mSecPoint.modify(mFirPoint);
        }
        if (mRectF != null) {
            mRectF.modify(mCurX - offsetSub, mCurY - offsetSub, mCurX + offsetSub, mCurY + offsetSub);

        }
    }

    @Override
    public void startAnim() {
        mAnimator = new AnimatorValue();
        mAnimator.setDuration(mDuration);
        mAnimator.setValueUpdateListener((animatorValue, v) -> {
            float factor = (float) v;
            //竖线
            float my = (int) (mFirPoint.getPointY() - mLineLength * factor);
            mSecPoint.modify(mSecPoint.getPointX(), my);
            if (factor > 0.333f) {
                //2/3处开始画圆
                float zoroToOne = (factor - 0.333f) * 3 / 2;
                mSweepAngle = -(int) (360 * zoroToOne);
            }
        });
        mAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (animationCallBack != null) {
                    animationCallBack.onLetterAnimatorEnd();
                }
            }
        });
        mAnimator.start();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        //竖线
        canvas.drawLine(mFirPoint.getPointX(), mFirPoint.getPointY(), mSecPoint.getPointX(), mSecPoint.getPointY(), mPaint);
        //圆
        canvas.drawArc(mRectF, new Arc(mStartAngle, mSweepAngle, false), mPaint);
    }
}
